package com.haidechizi.leetcode;

/**
 * @author: wangdejun
 * @create: 2020-04-02 11:49
 **/
public class _10_斐波那契数列 {

    public int fib(int n) {
        if (n <= 1) {
            return n;
        }
        int pre1 = 0;
        int pre2 = 1;
        int result = 0;
        for (int i =2;i <=n;i++) {
            result = pre1+pre2;
            pre1 = pre2;
            pre2 = result;
        }

        return result;
    }


    public int fib2(int n) {
        if (n <= 1) {
            return n;
        }
        int[] fib = new int[n + 1];
        fib[1] = 1;
        for (int i = 2; i <= n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }
        return fib[n];
    }


    public int fib1(int n) {
        if (n <= 1) {
            return n;
        }
        return fib1(n - 1) + fib1(n - 2);
    }
}
